<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tour extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('parser');
        $this->template->set_template('home');
        //$this->template->add_region('new_tours');
        $this->load->model('tour_model', 'tour', true);
        $this->load->model('tcategory_model', 'category', true);
        $this->load->helper('form');
    }

    function index() {
        $data = array();

        $tours = $this->tour->getAll(0, 3);
        $data['tours'] = $tours;
        //print_r($tours);
        $new_tours = $this->parser->parse('default/front_end/new_tour', $data, true);
        $this->template->add_region('new_tours');
        $this->template->write('new_tours', $new_tours);

        $_outbound_ids = $this->category->getAllIDByParentId(2);
        // print_r($_outbound_ids);
        $outbound_ids = array();
        for ($i = 0; $i < count($_outbound_ids); $i++)
            $outbound_ids[] = $_outbound_ids[$i]['id'];
        $outbound_ids = implode(',', $outbound_ids);
        $outbound_tour = $this->tour->findByCategory($outbound_ids, 0, 10);
        $data['outbound_tour'] = $outbound_tour;

        $_domestic_ids = $this->category->getAllIDByParentId(1);
        //print_r($_domestic_ids);
        $domestic_ids = array();
        for ($i = 0; $i < count($_domestic_ids); $i++)
            $domestic_ids[] = $_domestic_ids[$i]['id'];

        $domestic_ids = implode(',', $domestic_ids);
        $domestic_tour = $this->tour->findByCategory($domestic_ids, 0, 10);
        $data['domestic_tour'] = $domestic_tour;
        $data['image_path'] = base_url() . TOUR_IMAGE_PATH;
        $this->template->parse_view('content', 'default/front_end/view_home', $data);
        //$this->buildLeftContent();

        $this->template->render();
    }

    function detail($id) {
        $data = array();
        $data = $this->tour->findByID($id);
        //print_r($tour);
        //$data['tour'] = $tour;
        $data['base_url'] = base_url();
        $data['image_path'] = base_url() . TOUR_IMAGE_PATH;
        $this->template->parse_view('content', 'default/front_end/view_tour_detail', $data);
        //$this->template->write('content', '<div class="left_col">232fgfg fg fgf</div>');
        $this->template->render();
    }

    function view($cid = 0, $offset = 0, $limit = NUMBER_RECORD_PER_PRODUCT_PAGE) {
        if ($cid) {
            $_ids = $this->category->getAllIDByParentId($cid);
            // print_r($_outbound_ids);
            $ids = array();
            for ($i = 0; $i < count($_ids); $i++)
                $ids[] = $_ids[$i]['id'];
            $ids = implode(',', $ids);
            $tours = $this->tour->findByCategory($ids, 0, 10);
        } else {
            $tours = $this->tour->getAll($offset, $limit);
        }

        $data['tours'] = $tours;
        $data['image_path'] = base_url() . TOUR_IMAGE_PATH;
        $this->template->parse_view('content', 'default/front_end/view_tour', $data);
        //$this->template->write('content', '<div class="left_col">232fgfg fg fgf</div>');
        $this->template->render();
    }

    function booking($id) {

        $id = intval($id);
        if (!$id) {
            redirect('tour/view');
            return;
        }

        $this->load->helper('string');
        $uid = $this->input->post('txtUID');

        $data = $this->tour->findByID($id);
        $data['tour_id'] = $data['id'];
        $data['company_name'] = $this->input->post('txtCompanyName');
        $data['lastname'] = $this->input->post('txtLastname');
        $data['firstname'] = $this->input->post('txtFirstname');
        $data['title'] = $this->input->post('ddlTitle');
        $data['email'] = $this->input->post('txtEmail');
        $data['address'] = $this->input->post('txtAddress');
        $data['phone'] = $this->input->post('txtPhone');
        $data['fax'] = $this->input->post('txtFax');
        $data['departure_date'] = $this->input->post('txtDeparture');
        $data['adult'] = $this->input->post('txtAdultsNumber');
        $data['children6'] = $this->input->post('txtChildren6');
        $data['children11'] = $this->input->post('txtChildren11');
        $data['single_room'] = $this->input->post('txtSingleRoom');
        $data['double_room'] = $this->input->post('txtDoubleRoom');
        $data['triple_room'] = $this->input->post('txtTripleRoom');
        $data['accommodation'] = $this->input->post('ddlAccommodation');
        $data['transport'] = $this->input->post('ddlTransport');
        $data['requirement'] = $this->input->post('txtRequirement');
        //$data['transport'] = $this->input->post('ddlTransport');
        $data['error_message'] = '';
        
        if($uid == $this->session->userdata('uid')){
            //echo "SESSION = NHAU";
            $this->load->helper(array('form', 'url'));

            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('txtFirstname', 'Firstname', 'required|min_length[2]|max_length[120]');
            $this->form_validation->set_rules('txtLastname', 'txtLastname', 'required|min_length[2]|max_length[120]');
            $this->form_validation->set_rules('txtEmail', 'Email', 'required|valid_email');
            $this->form_validation->set_rules('txtPhone', 'Phone', 'required|min_length[10]|max_length[11]');
            $this->form_validation->set_rules('txtFax', 'lang:vi_description', 'min_length[10]');
           

            if ($this->form_validation->run() == FALSE) {
               $data['error_message'] = '<div stype="margin-left:100px; width=500px; color:red">' . validation_errors() . '</div>' ;
            }
            else{
                $this->load->model('book_tour_model', 'book', true);
                $data['status'] = 1;
                $data['add_date'] = date('Y-m-d H:i:s', time());
                $data['upd_date'] = date('Y-m-d H:i:s', time());
                $id = $this->book->create($data);
                if($id){
                    $data['message'] = "Quý khách đã đặt tour thành công. Chúng tôi sẽ sớm liên hệ lại với quý khách.
                                        Cảm ơn quý khách đã sữ dụng dịch vụ của chúng tôi.";
                    $this->session->unset_userdata('uid');

                    /*
                    $this->load->library('email');
                    
                    $config['protocol'] = 'smtp';
                    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
                    $config['smtp_port'] = '465';
                    $config['smtp_user'] = 'pvkhoi@gmail.com';
                    $config['smtp_pass'] = '02329120';
                    $config['charset'] = 'utf-8';


                    $config['wordwrap'] = TRUE;

                    $this->email->initialize($config);
                    $this->email->from('pvkhoi@gmail.com', 'Phạm Minh Khởi');
                    $this->email->to('pmkhoi@gmail.com');
                    $this->email->subject('This is my subject');
                    $this->email->message('This is my message');
                    //$this->email->send();
                    if($this->email->send())
                    {
                        echo 'Your email was sent, successfully.';
                    }

                    else
                    {
                        show_error($this->email->print_debugger());
                    }
                    */
                }
                else{
                    $data['message'] = "Quý khách đặt tour chưa thành công. Xin quý khách vui lòng thử lại một lần nữa.";
                }

                $this->template->parse_view('content', 'default/front_end/view_message_book_tour', $data);
                //$this->template->write('content', '<div class="left_col">232fgfg fg fgf</div>');
                $this->template->render();
                return;
            }           
        }
       
            //$this->template->add_js('asset/ckeditor/ckeditor.js');
            $data['uid'] = random_string('alnum', 16);
            $this->session->set_userdata('uid', $data['uid']);

            $data_title = array();
            $data_title[1] = 'Ông';
            $data_title[2] = 'Bà';
            $data_title[3] = 'Anh';
            $data_title[4] = 'Chị';
            $data['ddlTitle'] = form_dropdown('ddlTitle', $data_title, $this->input->post('ddlTitle'), 'id= "ddlTitle"');

            $data_accommodation = array();
            $data_accommodation[1] = '1 Star';
            $data_accommodation[2] = '2 Star';
            $data_accommodation[3] = '3 Star';
            $data_accommodation[4] = '4 Star';
            $data_accommodation[5] = '5 Star';
            $data['ddlAccommodation'] = form_dropdown('ddlAccommodation', $data_accommodation, $this->input->post('ddlAccommodation'), 'id= "ddlAccommodation"');

            $data_transport = array();
            $data_transport[1] = 'Car';
            $data_transport[2] = 'Plane';
            $data_transport[3] = 'Cruise';
            $data_transport[4] = 'Train';
            $data_transport[5] = 'Bus';
            $data['ddlTransport'] = form_dropdown('ddlTransport', $data_transport, $this->input->post('ddlTransport'), 'id= "ddlTransport"');

            //print_r($data);

            $this->template->parse_view('content', 'default/front_end/view_booking', $data);
            $this->template->render();
      
    }

    function mail(){
        $this->load->library('email');

                    $config['protocol'] = 'smtp';
                    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
                    $config['smtp_port'] = '465';
                    $config['smtp_user'] = 'pvkhoi@gmail.com';
                    $config['smtp_pass'] = '02329120';
                    $config['charset'] = 'utf-8';


                    $config['wordwrap'] = TRUE;

                    $this->email->initialize($config);
                    $this->email->from('pvkhoi@gmail.com', 'Phạm Minh Khởi');
                    $this->email->to('pmkhoi@gmail.com');
                    $this->email->subject('This is my subject');
                    $this->email->message('This is my message');
                    //$this->email->send();
                    if($this->email->send())
                    {
                        echo 'Your email was sent, successfully.';
                    }

                    else
                    {
                        show_error($this->email->print_debugger());
                    }
    }

    function buildLeftContent() {
        $data['menuitems'] = array(
            array(
                'status' => 'active',
                'click_function' => 'get_list(current_from, this);',
                'text' => 'Add New Child',
                'href' => site_url('children/add_child'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'create_user(this);',
                'text' => 'View All Children ',
                'href' => site_url('children/view_all'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_logs(this);',
                'text' => 'View Current Children ',
                'href' => site_url('children/view_current'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'login_logs(this);',
                'text' => 'View Graduated Children ',
                'href' => site_url('children/view_graduated'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_config(this);',
                'text' => 'View Withdrawn Children ',
                'href' => site_url('children/view_withdraw'),
                'subitem' => array('a' => 'a')
            )
        );
        $this->load->library('parser');
        $this->parser->parse('default/front_end/menu_left', $data);
        $this->template->parse_view('header', 'default/front_end/menu_left', $data);
    }

}

?>